﻿@page "/Device"
@page "/DeviceGroup"
@page "/DeviceLog/{Id:guid?}"
@page "/DeviceLog/{Action}/{Id:guid?}"
@page "/DeviceAction/{Id:guid?}"
@page "/DeviceAction/{Action}/{Id:guid?}"
@page "/DeviceTrace/{Id:guid?}"
@page "/DeviceTrace/{Action}/{Id:guid?}"

@using Gurux.DLMS.AMI.Shared.DIs
@using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication
@using Gurux.DLMS.AMI.Shared.DTOs.Authentication
@using Gurux.DLMS.AMI.Shared
@using Gurux.DLMS.AMI.Shared.Enums
@using Gurux.DLMS.AMI.Shared.DTOs
@using System.Diagnostics

@attribute [Authorize(Roles = "Admin, Device, DeviceManager, DeviceGroup, DeviceGroupManager, DeviceError, DeviceErrorManager, DeviceAction, DeviceActionManager, DeviceTrace, DeviceTraceManager")]

@inject NavigationManager NavigationManager
@inject HttpClient Http
@inject IGXNotifier2 Notifier
@implements IDisposable

<!--If user adds a new item or modifies the existing one.-->
@if (string.Compare(Action, "Add", true) == 0 || (Id != null && Path != null && string.Compare(Action, "Edit", true) == 0))
{
    @if (string.Compare(Path, "Device", true) == 0)
    {
        <DeviceTab Action="@Action" Id="@Id"></DeviceTab>
    }
    else if (string.Compare(Path, "DeviceGroup", true) == 0)
    {
        <DeviceGroupTab Action="@Action" Id="@Id"></DeviceGroupTab>
    }
    else if (string.Compare(Path, "DeviceLog", true) == 0)
    {
        <DeviceLog Action="@Action" Id="@Id"></DeviceLog>
    }
    else if (string.Compare(Path, "DeviceAction", true) == 0)
    {
        <DeviceAction Action="@Action" Id="@Id"></DeviceAction>
    }
    else if (string.Compare(Path, "DeviceTrace", true) == 0)
    {
        <DeviceTrace Action="@Action" Id="@Id"></DeviceTrace>
    }
}
else
{
    <TabControl Active="@Path" SelectionChanged="@SelectionChanged">
        <TabTextTemplate>
            <span class=@context.Icon aria-hidden="true">
                @if (context.PadgeCount != 0)
                {
                    <span class="@context.GetPadgeClass()">
                        @context.PadgeCount
                        <span class="visually-hidden">@context.Padge</span>
                    </span>
                }
            </span>
            @context.Text
        </TabTextTemplate>
        <ChildContent>
            <AuthorizeView Roles=@GXRoles.ToString(GXRoles.Admin, GXRoles.Device, GXRoles.DeviceManager)>
                <TabPage Path="Device" Text=@Properties.Resources.Devices Icon="oi oi-tablet">
                    <div style="position: relative; width: 100%; ">
                        <Devices Id="@Id"></Devices>
                    </div>
                </TabPage>
            </AuthorizeView>
            <AuthorizeView Roles=@GXRoles.ToString(GXRoles.Admin, GXRoles.DeviceGroup, GXRoles.DeviceGroupManager)>
                <TabPage Path="DeviceGroup" Text=@Properties.Resources.DeviceGroups>
                    <div style="position: relative; width: 100%; ">
                        <DeviceGroups Id="@Id"></DeviceGroups>
                    </div>
                </TabPage>
            </AuthorizeView>
            <AuthorizeView Roles=@GXRoles.ToString(GXRoles.Admin, GXRoles.DeviceLog, GXRoles.DeviceLogManager)>
                <TabPage Path="DeviceLog"
                         Text=@Properties.Resources.DeviceErrors
                         Padge="New device errors"
                         PadgeCount="@_log.GetCount()"
                         PadgeType="@_log.GetTraceLevel()"
                         Icon="oi oi-warning">
                    <div style="position: relative; width: 100%; ">
                        <DeviceLogs Id="@Id"></DeviceLogs>
                    </div>
                </TabPage>
            </AuthorizeView>
            <AuthorizeView Roles=@GXRoles.ToString(GXRoles.Admin, GXRoles.DeviceAction, GXRoles.DeviceActionManager)>
                <TabPage Path="DeviceAction" Text=@Properties.Resources.DeviceActions Icon="oi oi-info">
                    <div style="position: relative; width: 100%; ">
                        <DeviceActions Id="@Id"></DeviceActions>
                    </div>
                </TabPage>
            </AuthorizeView>
            <AuthorizeView Roles=@GXRoles.ToString(GXRoles.Admin, GXRoles.DeviceTrace, GXRoles.DeviceTraceManager)>
                <TabPage Path="DeviceTrace" Text=@Properties.Resources.DeviceTraces Icon="oi oi-transfer">
                    <div style="position: relative; width: 100%; ">
                        <DeviceTraces Id="@Id"></DeviceTraces>
                    </div>
                </TabPage>
            </AuthorizeView>
        </ChildContent>
    </TabControl>
}
<br />
@code {
    /// <summary>
    /// User action.
    /// </summary>
    [Parameter]
    public string? Action { get; set; }
    /// <summary>
    /// Selected item.
    /// </summary>
    [Parameter]
    public Guid? Id { get; set; }

    private string? Path { get; set; }

    /// <summary>
    /// Stamp.
    /// </summary>
    private GXUserStamp _log = new GXUserStamp();

    /// <summary>
    /// User has selected a new tab.
    /// </summary>
    /// <param name="page">Tab page.</param>
    void SelectionChanged(TabPage page)
    {
        Path = page.Path;
    }

    protected override void OnInitialized()
    {
        //Get selected tab from the url.
        Path = NavigationManager.ToBaseRelativePath(NavigationManager.Uri);
        _log = Notifier.GetUserStamps(TargetType.DeviceError);
        Notifier.On<IEnumerable<GXUserStamp>>(this, nameof(IGXHubEvents.StampUpdate), (stamps) =>
        {
            bool updated = false;
            foreach (var it in stamps)
            {
                if (it.TargetType == TargetType.DeviceError)
                {
                    updated = true;
                }
            }
            if (updated)
            {
                StateHasChanged();
                StateHasChanged();
            }
        });
    }

    public void Dispose()
    {
        Notifier.RemoveListener(this);
    }
}